<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Config;

class Login extends Controller{
    //
    public function index(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $remember = $data['remember'] ?? 0;
            $result = $this->validate($data, 'app\admin\validate\Login');
            if(true !== $result){
                $this->assign('message', $result);
                return $this->fetch();
            }
            $res = model('Admin')->login($data['username'], $data['password'], $remember);
            if($res['code'] == 0){
                $this->redirect('Index/index');
            }
            $this->assign('message', $res['msg']);
        }
        //$this->view->engine->layout(false);
        return $this->fetch();
    }
    //
    public function logout(){
        $session_name = Config::get('app.session_name');
        if($session_name){
            model('Admin')->logout($session_name);
        }
        $this->redirect('Login/index');
    }
}